package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.ClubPetrolDaily;
import cn.pconline.autoclub.entity.ClubTotalMember;
import cn.pconline.autoclub.entity.MemberStat;
import cn.pconline.autoclub.entity.User;
import java.util.List;
import org.gelivable.dao.CacheFirstLevel;
import org.gelivable.dao.CacheSecondLevel;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 作者 guoqiang
 * @version 2014-5-8
 */
public class CacheService {

    @Autowired
    private CacheSecondLevel cacheSecondLevel;
    @Autowired
    private CacheFirstLevel cacheFirstLevel;
    @Autowired
    private ClubService clubService;
    @Autowired
    private UserService userService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SendMsgService sendMsgService;

    /**
     * 清理车友会一二级缓存----主要更新排名字段
     */
    public void initClubCache() {

        List<Club> listClub = clubService.initCacheClub();
        if (listClub != null && !listClub.isEmpty()) {
            for (Club club : listClub) {
                String key = club.getClass().getSimpleName() + "-" + club.getClubId();
                cacheFirstLevel.delete(key);
                cacheSecondLevel.delete(key);
            }
        }
    }

    /**
     * 清楚用户数据缓存
     */
    public void initUserCache() {
        List<User> list = userService.initCacheUser();
        if (list != null && !list.isEmpty()) {
            for (User user : list) {
                String key = user.getClass().getSimpleName() + "-" + user.getUid();
                cacheFirstLevel.delete(key);
                cacheSecondLevel.delete(key);
            }
        }
    }

    public void initMemberStat() {

        List<MemberStat> list = memberService.initCacheMemberStat();
        if (list != null && !list.isEmpty()) {
            for (MemberStat memberStat : list) {
                String key = memberStat.getClass().getSimpleName() + "-" + memberStat.getMemberStatId();
                cacheFirstLevel.delete(key);
            }
        }
    }

    public void initClubDailyStat() {

        List<ClubPetrolDaily> list = clubService.initClubDailyStat();
        if (list != null && !list.isEmpty()) {
            for (ClubPetrolDaily clubPetrolDaily : list) {
                String key = clubPetrolDaily.getClass().getSimpleName() + "-" + clubPetrolDaily.getPetrolDailyId();
                cacheFirstLevel.delete(key);
                cacheSecondLevel.delete(key);
            }
        }
    }

    public void initToalMember() {

        List<ClubTotalMember> list = memberService.initToalMember();
        if (list != null && !list.isEmpty()) {
            for (ClubTotalMember clubTotalMember : list) {
                String key = clubTotalMember.getClass().getSimpleName() + "-" + clubTotalMember.getClubId();
                cacheFirstLevel.delete(key);
                cacheSecondLevel.delete(key);
            }
        }
    }

    public void initMemberInfo() {

        List<ClubMember> list = memberService.initMemberInfo();
        if (list != null && !list.isEmpty()) {
            for (ClubMember m : list) {
                String key = m.getClass().getSimpleName() + "-" + m.getId();
                cacheFirstLevel.delete(key);
                cacheSecondLevel.delete(key);
            }
        }
    }
}
